// 2012. 数组美丽值求和
// AC: https://leetcode.cn/problems/sum-of-beauty-in-the-array/submissions/610828121/

class Solution {
public:
    int sumOfBeauties(vector<int>& nums) {
        int n=nums.size();
        vector<int> v(n,0);
        v[n-1]=nums[n-1];
        for(int i=n-2;i>0;--i)
            v[i]=min(nums[i],v[i+1]);
        int t=nums[0],sum=0;
        for(int i=1;i<n-1;++i){
            if(nums[i]>t&&nums[i]<v[i+1])
                sum+=2;
            else if(nums[i]>nums[i-1]&&nums[i]<nums[i+1])
                sum+=1;
            t=max(t,nums[i]);
        }
        return sum;
    }
};